home *** CD-ROM | disk | FTP | other *** search
/ Eagles Nest BBS 8 / Eagles_Nest_Mac_Collection_Disc_8.TOAST / Developer Tools⁄Additions / PrPrimerVol1 / (CH. 7) Pager / Pager.c next >
Encoding:
C/C++ Source or Header  |  1990-05-04  |  6.4 KB  |  291 lines  |  [TEXT/KAHL]

  1. #define        BASE_RES_ID            400
  2. #define        NIL_POINTER            0L
  3. #define        MOVE_TO_FRONT        -1L
  4. #define        REMOVE_ALL_EVENTS    0
  5. #define        SCROLL_BAR_PIXELS    16
  6. #define        DRAG_THRESHOLD        30
  7. #define        NIL_ACTION_PROC        NIL_POINTER
  8.  
  9. #define        MIN_SLEEP            0L
  10. #define        NIL_MOUSE_REGION    0L
  11.  
  12. #define        WNE_TRAP_NUM        0x60
  13. #define        UNIMPL_TRAP_NUM        0x9F
  14.  
  15. #define        ERROR_ALERT_ID        BASE_RES_ID+1
  16. #define        NO_WIND                BASE_RES_ID
  17. #define        NO_PICTS            BASE_RES_ID+1
  18. #define        CANT_LOAD_PICT        BASE_RES_ID+2
  19.  
  20. #define        NIL_STRING                    "\p"
  21. #define        NIL_TITLE                    NIL_STRING
  22. #define        VISIBLE                        TRUE
  23. #define        START_VALUE                    1
  24. #define        MIN_VALUE                    1
  25. #define        NIL_REF_CON                    NIL_POINTER
  26. #define        HOPELESSLY_FATAL_ERROR        "\pGame over, man!"
  27.  
  28. WindowPtr        gPictWindow;
  29. ControlHandle    gScrollBarHandle;
  30. Boolean            gDone, gWNEImplemented;
  31. EventRecord        gTheEvent;
  32. Rect            gDragRect;
  33.  
  34. pascal void ScrollProc();
  35.  
  36. /******************************** main *********/
  37.  
  38. main()
  39. {
  40.     ToolBoxInit();
  41.     WindowInit();
  42.     SetUpDragRect();
  43.     SetUpScrollBar();
  44.     MainLoop();
  45. }
  46.  
  47. /*********************************** ToolBoxInit */
  48.  
  49. ToolBoxInit()
  50. {
  51.     InitGraf( &thePort );
  52.     InitFonts();
  53.     FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
  54.     InitWindows();
  55.     InitMenus();
  56.     TEInit();
  57.     InitDialogs( NIL_POINTER );
  58.     InitCursor();
  59. }
  60.  
  61. /******************************** WindowInit *********/
  62.  
  63. WindowInit()
  64. {
  65.     if ( ( gPictWindow = GetNewWindow( BASE_RES_ID, NIL_POINTER, MOVE_TO_FRONT ) ) == NIL_POINTER )
  66.         ErrorHandler( NO_WIND );
  67.     
  68.     SelectWindow( gPictWindow );
  69.     ShowWindow( gPictWindow );
  70.     SetPort( gPictWindow );
  71. }
  72.  
  73. /******************************** SetUpDragRect *********/
  74.  
  75. SetUpDragRect()
  76. {
  77.     gDragRect = screenBits.bounds;
  78.     gDragRect.left += DRAG_THRESHOLD;
  79.     gDragRect.right -= DRAG_THRESHOLD;
  80.     gDragRect.bottom -= DRAG_THRESHOLD;
  81. }
  82.  
  83. /**********************************     SetUpScrollBar     *******/
  84.  
  85. SetUpScrollBar()
  86. {
  87.     Rect    vScrollRect;
  88.     int        numPictures;
  89.     
  90.     if ( ( numPictures = CountResources( 'PICT' ) ) <= 0 )
  91.         ErrorHandler( NO_PICTS );
  92.     
  93.     vScrollRect = gPictWindow->portRect;
  94.     vScrollRect.top -= 1;
  95.     vScrollRect.bottom += 1;
  96.     vScrollRect.left = vScrollRect.right - SCROLL_BAR_PIXELS+1;
  97.     vScrollRect.bottom += 1;
  98.     gScrollBarHandle = NewControl( gPictWindow, &vScrollRect, NIL_TITLE, VISIBLE, START_VALUE, MIN_VALUE, numPictures, scrollBarProc, NIL_REF_CON );
  99. }
  100.  
  101. /******************************** MainLoop *********/
  102.  
  103. MainLoop()
  104. {
  105.     gDone = FALSE;
  106.     gWNEImplemented = ( NGetTrapAddress( WNE_TRAP_NUM, ToolTrap ) != NGetTrapAddress( UNIMPL_TRAP_NUM, ToolTrap ) );
  107.     while ( gDone == FALSE )
  108.         {
  109.         HandleEvent();
  110.         }
  111. }
  112.  
  113. /************************************* HandleEvent   */
  114.  
  115. HandleEvent()
  116. {
  117.     if ( gWNEImplemented )
  118.         WaitNextEvent( everyEvent, &gTheEvent, MIN_SLEEP, NIL_MOUSE_REGION );
  119.     else
  120.         {
  121.         SystemTask();
  122.         GetNextEvent( everyEvent, &gTheEvent );
  123.         }
  124.     
  125.     switch ( gTheEvent.what )
  126.         {
  127.         case mouseDown:
  128.             HandleMouseDown();
  129.             break;
  130.         case updateEvt:
  131.             BeginUpdate( gTheEvent.message );
  132.             DrawControls( gTheEvent.message );
  133.             UpdateMyWindow( gTheEvent.message );
  134.             EndUpdate( gTheEvent.message );
  135.             break;
  136.         }
  137. }
  138.  
  139. /************************************* HandleMouseDown */
  140.  
  141. HandleMouseDown()
  142. {
  143.     WindowPtr        whichWindow;
  144.     short int        thePart;
  145.     Point            thePoint;
  146.     ControlHandle    theControl;
  147.     
  148.     thePart = FindWindow( gTheEvent.where, &whichWindow );
  149.     
  150.     switch( thePart )
  151.         {
  152.         case inSysWindow:
  153.             SystemClick( &gTheEvent, whichWindow );
  154.             break;
  155.         case inDrag:
  156.             DragWindow( whichWindow, gTheEvent.where, &gDragRect );
  157.             break;
  158.         case inContent:
  159.             thePoint = gTheEvent.where;
  160.             GlobalToLocal( &(thePoint) );
  161.             thePart = FindControl( thePoint, whichWindow, &theControl );
  162.             
  163.             if ( theControl == gScrollBarHandle )
  164.                 {
  165.                 if ( thePart == inThumb )
  166.                     {
  167.                     thePart = TrackControl( theControl, thePoint, NIL_ACTION_PROC );
  168.                     UpdateMyWindow( whichWindow );
  169.                     }
  170.                 else
  171.                     {
  172.                     thePart = TrackControl( theControl, thePoint, &ScrollProc );
  173.                     UpdateMyWindow( whichWindow );
  174.                     }
  175.                 }
  176.             break;
  177.         case inGoAway:
  178.             gDone = TRUE;
  179.             break;
  180.         }
  181. }
  182.  
  183. /**********************************     ScrollProc    *******/
  184.  
  185. pascal void ScrollProc(theControl, theCode)
  186.  
  187. ControlHandle    theControl;
  188. int                theCode;
  189.  
  190. {
  191.     int        curControlValue, maxControlValue, minControlValue;
  192.     
  193.     maxControlValue = GetCtlMax( theControl );
  194.     curControlValue = GetCtlValue( theControl );
  195.     minControlValue = GetCtlMin( theControl );
  196.     
  197.     switch ( theCode )
  198.         {
  199.         case inPageDown:
  200.         case inDownButton:
  201.             if ( curControlValue < maxControlValue )
  202.                 {
  203.                 curControlValue += 1;
  204.                 }
  205.                 break;
  206.         case inPageUp:
  207.         case inUpButton:
  208.             if ( curControlValue > minControlValue );
  209.                 {
  210.                 curControlValue -= 1;
  211.                 }
  212.         }
  213.         
  214.         SetCtlValue( theControl, curControlValue );
  215. }
  216.  
  217. /**********************************     UpdateMyWindow     *******/
  218.  
  219. UpdateMyWindow( drawingWindow )
  220.  
  221. WindowPtr    drawingWindow;
  222.  
  223. {
  224.     PicHandle    currentPicture;
  225.     Rect        drawingClipRect, myRect;
  226.     RgnHandle    tempRgn;
  227.     
  228.     tempRgn = NewRgn();
  229.     GetClip( tempRgn );
  230.     
  231.     myRect = drawingWindow->portRect;
  232.     myRect.right -= SCROLL_BAR_PIXELS;
  233.     EraseRect( &myRect );
  234.     
  235.     currentPicture = (PicHandle)GetIndResource( 'PICT', GetCtlValue( gScrollBarHandle ) );
  236.     
  237.     if ( currentPicture == NIL_POINTER )
  238.         ErrorHandler( CANT_LOAD_PICT );
  239.     
  240.     CenterPict( currentPicture, &myRect );
  241.     
  242.     drawingClipRect = drawingWindow->portRect;
  243.     drawingClipRect.right -= SCROLL_BAR_PIXELS;
  244.     ClipRect( &drawingClipRect );
  245.     
  246.     DrawPicture( currentPicture, &myRect );
  247.     
  248.     SetClip( tempRgn );
  249.     DisposeRgn( tempRgn );
  250. }
  251.  
  252. /******************************** CenterPict *********/
  253.  
  254. CenterPict( thePicture, myRectPtr )
  255.  
  256. PicHandle    thePicture;
  257. Rect        *myRectPtr;
  258.  
  259. {
  260.     Rect    windRect, pictureRect;
  261.     
  262.     windRect = *myRectPtr;
  263.     pictureRect = (**( thePicture )).picFrame;
  264.     myRectPtr->top = (windRect.bottom - windRect.top - (pictureRect.bottom - pictureRect.top)) / 2 + windRect.top;
  265.     myRectPtr->bottom = myRectPtr->top + (pictureRect.bottom - pictureRect.top);
  266.     myRectPtr->left = (windRect.right - windRect.left - (pictureRect.right - pictureRect.left)) / 2 + windRect.left;
  267.     myRectPtr->right = myRectPtr->left + (pictureRect.right - pictureRect.left);
  268. }
  269.  
  270. /******************************** ErrorHandler *********/
  271.  
  272. ErrorHandler( stringNum )
  273.  
  274. int    stringNum;
  275.  
  276. {
  277.     StringHandle errorStringH;
  278.     
  279.     if ( ( errorStringH = GetString( stringNum ) ) == NIL_POINTER )
  280.         ParamText( HOPELESSLY_FATAL_ERROR, NIL_STRING, NIL_STRING, NIL_STRING );
  281.         
  282.     else
  283.         {
  284.         HLock( errorStringH );
  285.         ParamText( *errorStringH, NIL_STRING, NIL_STRING, NIL_STRING );
  286.         HUnlock( errorStringH );
  287.         }
  288.     
  289.     StopAlert( ERROR_ALERT_ID, NIL_POINTER );
  290.     ExitToShell();
  291. }